package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.GroupNotice;
import cn.stylefeng.guns.sys.modular.system.mapper.GroupNoticeMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeResult;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-07-26
 */
@Service
public class GroupNoticeServiceImpl extends ServiceImpl<GroupNoticeMapper, GroupNotice> implements GroupNoticeService {

    @Override
    public void add(GroupNoticeParam param) {
        GroupNotice entity = getEntity(param);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(GroupNoticeParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(GroupNoticeParam param) {
        GroupNotice oldEntity = getOldEntity(param);
        GroupNotice newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GroupNoticeResult findBySpec(GroupNoticeParam param) {
        return null;
    }

    @Override
    public List<GroupNoticeResult> findListBySpec(GroupNoticeParam param) {
        List<GroupNoticeResult> groupNoticeResultList=this.baseMapper.findListBySpec(param);
        return groupNoticeResultList;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GroupNoticeParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findNoticePagePushData(GroupNoticeParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findNoticePagePushData(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findNoticePageReceiveData(GroupNoticeParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findNoticePageReceiveData(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findNoticePageBeenSeenData(GroupNoticeParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findNoticePageBeenSeenData(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public GroupNoticeResult findOneNoticeById(GroupNoticeParam param) {
        GroupNoticeResult result = this.baseMapper.findOneNoticeById(param);

        List<String> userNameList=this.baseMapper.findUserNameList(result.getNoticeId());
        if(CollectionUtil.isNotEmpty(userNameList)){
            result.setReceiveUserNameList(userNameList);
        }
        return result;
    }

    @Override
    public ResponseData delNotice(GroupNoticeParam param) {
        GroupNotice notice = this.getById(param.getNoticeId());
        if (notice != null) {
            if (!notice.getPortalUserId().equals(param.getPortalUserId())) {
                return ResponseData.error(412, "请求失败，此条通知是" + notice.getPortalUserName() + "创建，其他人不能删除");
            }
            notice.setDelFlag(1);
            this.saveOrUpdate(notice);
        }
        return ResponseData.success();
    }

    private Serializable getKey(GroupNoticeParam param) {
        return param.getNoticeId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GroupNotice getOldEntity(GroupNoticeParam param) {
        return this.getById(getKey(param));
    }

    private GroupNotice getEntity(GroupNoticeParam param) {
        GroupNotice entity = new GroupNotice();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
